<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="../css/register.css">
    <script>

    function Focus(temp) {
        document.querySelector(`.${temp}_error_msg`).innerHTML = "";
        document.querySelector(`.${temp}_error_img`).classList.remove("errorImg");
        document.querySelector(`.${temp}_error_msg`).classList.remove("errorMessage");
        document.querySelector(`.${temp}_error_msg`).classList.remove("okMessage");
        document.querySelector(`.${temp}_error_img`).classList.remove("okImg");
        document.querySelector(`.${temp}_error_img`).classList.remove("okImg");
    }
    function errorBlur(temp) {
        document.querySelector(`.${temp}_error_msg`).classList.add("errorMessage");
        document.querySelector(`.${temp}_error_img`).classList.add("errorImg");
    }
    function okBlur(temp) {
        document.querySelector(`.${temp}_error_msg`).classList.add("okMessage");
        document.querySelector(`.${temp}_error_img`).classList.add("okImg");
    }

    function userNameFocus() {
        Focus('userName');
    }
    function userNameBlur() {
        let regExp = /^\w+$/;
        let userName = document.getElementById("userName").value;
        if (!regExp.test(userName)) {
            document.querySelector(".userName_error_msg").innerHTML = "用户名格式不正确！";
            errorBlur('userName');
        } else {
            document.querySelector(".userName_error_msg").innerHTML = "用户名格式符合要求！";
            okBlur('userName');
        }
    }

    function userNickFocus() {
        Focus('userNick');
    }
    function userNickBlur() {
        let userNick = document.getElementById("userNick").value;
        if (userNick==""|| userNick==null) {
            document.querySelector(".userNick_error_msg").innerHTML = "昵称格式不能为空！";
            errorBlur('userNick');
        } else {
            document.querySelector(".userNick_error_msg").innerHTML = "昵称格式符合要求！";
            okBlur('userNick');
        }
    }

    function passwordFocus() {
        Focus('password');
    }
    function passwordBlur() {
        let regExp = /^\w{8,20}$/;
        let password = document.getElementById("password").value;
        if (!regExp.test(password)) {
            document.querySelector(".password_error_msg").innerHTML = "密码格式不正确！";
            errorBlur('password');
        } else {
            document.querySelector(".password_error_msg").innerHTML = "密码格式符合要求！";
            okBlur('password');
        }
    }

    function rePasswordFocus() {
        Focus('rePassword');
    }
    function rePasswordBlur() {
        let rePassword = document.getElementById("rePassword").value;
        let password = document.getElementById("password").value;
        if (password != rePassword) {
            document.querySelector(".rePassword_error_msg").innerHTML = "两次输入的密码请一致！";
            errorBlur('rePassword');
        } else {
            document.querySelector(".rePassword_error_msg").innerHTML = "密码格式符合要求！";
            okBlur('rePassword');
        }
    }

    function phoneFocus() {
        Focus('phone');
    }
    function phoneBlur() {
        let regExp = /^1\d{10}$/;
        let phone = document.getElementById("phone").value;
        if (!regExp.test(phone)) {
            document.querySelector(".phone_error_msg").innerHTML = "手机号格式不正确！";
            errorBlur('phone');
        } else {
            document.querySelector(".phone_error_msg").innerHTML = "手机号格式符合要求！";
            okBlur('phone');
        }
    }

    
    function emailFocus(){
        Focus('email')
    }
    function emailBlur(){
        let regExp = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        if (!regExp.test(document.getElementById("email").value)) {
            document.querySelector(".email_error_msg").innerHTML = "邮箱格式不正确！";
            errorBlur('email');
        } else {
            document.querySelector(".email_error_msg").innerHTML = "邮箱格式符合要求！";
            okBlur('email');
        }
    }



    function init() {
            //email检验置空
            document.getElementById("email").addEventListener("focus", emailFocus, false);
            //email检验
            document.getElementById("email").addEventListener("blur", emailBlur, false);

            //userName检验置空
            document.getElementById("userName").addEventListener("focus", userNameFocus, false);
            //userName检验
            document.getElementById("userName").addEventListener("blur", userNameBlur, false);

            //userNick检验置空
            document.getElementById("userNick").addEventListener("focus", userNickFocus, false);
            //userNick检验
            document.getElementById("userNick").addEventListener("blur", userNickBlur, false);

            //password检验置空
            document.getElementById("password").addEventListener("focus", passwordFocus, false);
            //password检验
            document.getElementById("password").addEventListener("blur", passwordBlur, false);

            //rePassword检验置空
            document.getElementById("rePassword").addEventListener("focus", rePasswordFocus, false);
            //rePassword检验
            document.getElementById("rePassword").addEventListener("blur", rePasswordBlur, false);

            //phone检验置空
            document.getElementById("phone").addEventListener("focus", phoneFocus, false);
            //phone检验
            document.getElementById("phone").addEventListener("blur", phoneBlur, false);

        }
        window.addEventListener("load", init, false);
    
    </script>
</head>
<body>
    <section>
        <aside class="img_box"></aside>
        <h1 style="text-align:center;margin-top:30px;">用户注册</h1>
        <fieldset>
            <legend hidden>用户注册</legend>
            <form action="" method="POST">
                <table>
                    <tbody>
                        <tr>
                            <td><label for="userName">用户名：</label></td>
                            <td><input type="text" name="userName" id="userName" autofocus></td>
                            <td>
                                <nav class="error_block">
                                    <div class="userName_error_img"></div>
                                    <div class="userName_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="userNick">昵称：</label></td>
                            <td><input type="text" name="userNick" id="userNick" ></td>
                            <td>
                                <nav class="error_block">
                                    <div class="userNick_error_img"></div>
                                    <div class="userNick_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="password">密码：</label></td>
                            <td><input type="password" name="password" id="password"></td>
                            <td>
                                <nav class="error_block">
                                    <div class="password_error_img"></div>
                                    <div class="password_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="rePassword">再输入一次：</label></td>
                            <td><input type="password" name="rePassword" id="rePassword"></td>
                            <td>
                                <nav class="error_block">
                                    <div class="rePassword_error_img"></div>
                                    <div class="rePassword_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="email">email：</label></td>
                            <td><input type="email" name="email" id="email"></td>
                            <td>
                                <nav class="error_block">
                                    <div class="email_error_img"></div>
                                    <div class="email_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td><label for="phone">手机号：</label></td>
                            <td>
                                <input type="text" name="phone" id="phone">
                            </td>
                            <td>
                                <nav class="error_block">
                                    <div class="phone_error_img"></div>
                                    <div class="phone_error_msg"></div>
                                </nav>
                            </td>
                        </tr>
                        <tr>
                            <td>性别：</td>
                            <td colspan="2">
                                <input type="radio" name="sex" value="1" id="man" checked>
                                <label for="man">男</label>&nbsp;
                                <input type="radio" name="sex" value="2" id="woman">
                                <label for="woman">女</label>
                            </td>
                        </tr>
                        <tr>
                            <td>爱好：</td>
                            <td colspan="2">
                                <input type="checkbox" name="hobbies" value="1" id="book">
                                <label for="book">读书</label>&nbsp;
                                <input type="checkbox" name="hobbies" value="2" id="music">
                                <label for="music">听音乐</label>&nbsp;
                                <input type="checkbox" name="hobbies" value="3">运动&nbsp;
                                <input type="checkbox" name="hobbies" value="4">写代码&nbsp;
                            </td>
                        </tr>
                        <tr>
                            <td>籍贯：</td>
                            <td colspan="2">
                                <select id="province"></select>&nbsp;
                                <select id="city"></select>
                            </td>
                        </tr>
                        <tr>
                            <td>照片：</td>
                            <td colspan="2">
                                <input type="file" name="photo" accept="image/*">
                            </td>
                        </tr>
                        <tr>
                            <td>生日：</td>
                            <td colspan="2">
                                <input type="date" name="birthday">
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <nav style="text-align: center;margin-top:30px;">
                                    <button type="reset">重置</button>&nbsp;&nbsp;
                                    <button type="submit">注册</button>
                                </nav>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </fieldset>
    </section>
</body>

</html>